<template>
  <div class="app-container">
    <uploader :autoStart="true" :options="options" class="uploader-example" :fileStatusText="fileStatusText" @file-added="added">
      <uploader-unsupport></uploader-unsupport>
      <uploader-drop>
        <p>拖动文件到这里</p>
        <uploader-btn>选择</uploader-btn>
      </uploader-drop>
      <uploader-list></uploader-list>
    </uploader>
    <el-table v-loading="!pload" border :data="tableData" style="margin-top:50px;">
      <el-table-column property="name" label="类型" align="center" show-overflow-tooltip width="120"></el-table-column>
      <el-table-column property="total" label="数据总数量" align="center" show-overflow-tooltip width="120"></el-table-column>
      <el-table-column property="finished" label="导入成功数量" align="center" show-overflow-tooltip width="120"></el-table-column>
      <el-table-column property="error" label="导入失败数量" align="center" show-overflow-tooltip width="120"></el-table-column>
      <el-table-column property="name" label="整体进度">
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="18" :percentage="percent(scope.row)"></el-progress>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style>
.uploader-example {
  width: 880px;
  padding: 15px;
  margin: 40px auto 0;
  font-size: 12px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
}
.uploader-example .uploader-btn {
  margin-right: 4px;
}
.uploader-example .uploader-list {
  max-height: 440px;
  overflow: auto;
  overflow-x: hidden;
  overflow-y: auto;
}
</style>
<script>
import uploader from 'vue-simple-uploader'
import Vue from 'vue'
Vue.use(uploader)
import WS from '@/utils/websocket'
export default {
  data() {
    return {
      fileStatusText: {
        success: '上传成功',
        error: '出错了',
        uploading: '上传中',
        paused: '暂停中',
        waiting: '等待中'
      },
      options: {
        target: '/api1/article/uploadArticleByExcel',
        testChunks: true,
        chunkSize: 2 * 1024 * 1024,
        forceChunkSize: true,
        simultaneousUploads: 10,
        allowDuplicateUploads: true,
        singleFile: false,
        query: {
          siteId: this.$store.state.site.siteId
        },
        headers: {
          accessToken: this.$store.state.user.token
        },
        initFileFn: (file) => {
          // file.ignored = true
          // console.log(file)
        }
      },
      attrs: {
        accept: 'image/*'
      },
      tableData: [],
      pload: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    added(file) {
      if (/\.(xls|xlsx|zip)$/.test(file.name)) {
        if (/\.(xls|xlsx)$/.test(file.name) && file.size > 50 * 1024 * 1024) {
          this.$message({
            message: 'xls,xlsx文件须小于50M',
            type: 'warning'
          })
          file.ignored = true
        }
        if (/\.(zip)$/.test(file.name) && file.size > 2 * 1024 * 1024 * 1024) {
          this.$message({
            message: 'zip文件须小于2G',
            type: 'warning'
          })
          file.ignored = true
        }
      } else {
        file.ignored = true
        this.$message({
          message: '请选择xls,xlsx,zip文件',
          type: 'warning'
        })
      }
    },
    percent(row) {
      return parseInt((row.finished + row.error) / (row.total || 1) * 100)
    },
    async getData() {
      console.log(this.$S.pathWs)
      var url = this.$S.pathWs.replace(/^http:\/\//, 'ws://')
      url = url.replace(/^https:\/\//, 'wss://')
      var ws = new WS(url + '/api1/uploadArticleProcess')
      console.log(ws)
      ws.onopen = msg => {
        console.log(msg)
      }
      ws.onmessage = msg => {
        this.pload = true
        this.tableData = JSON.parse(msg.data)
        console.log(msg)
      }
      ws.onerror = msg => {
        console.log(msg)
      }
    }
  }
}
</script>